<!--  -->
<template>
  <div class="custom">
    <van-nav-bar
      @click-left="onClickLeft"
      title="新建收货地址"
      :left-arrow="true"
    />
    <van-form ref="form">
      <div class="content">
        <van-field
          v-model="name"
          :rules="[{ required: true, message: ' ' }]"
          placeholder="请填写收货人姓名"
        />
        <van-field
          v-model="number"
          :rules="[{ required: true, message: ' ' }]"
          placeholder="请填写收货人手机号"
        />
        <van-field
          readonly
          is-link
          :rules="[{ required: true, message: ' ' }]"
          name="datetimePicker"
          v-model="region"
          placeholder="所在地区"
          @click="showPicker = true"
        />
        <van-popup
          round
          v-model="showPicker"
          position="bottom"
          :style="{ height: '44%' }"
        >
          <van-area
            :area-list="areaList"
            @confirm="confirmsave"
            @cancel="showPicker = false"
          />
        </van-popup>
        <van-field
          v-model="address"
          rows="2"
          type="textarea"
          maxlength="50"
          :rules="[{ required: true, message: ' ' }]"
          placeholder="小区门牌等详细地址"
        />
      </div>
    </van-form>
    <div class="tag">
      <div class="label">标签</div>
      <div class="tag_main clearfix">
        <div class="clearfix">
          <div
            class="cell"
            :class="{ active: tagactive == index }"
            v-for="(item, index) in taglist"
            :key="index"
            @click="tagactive = index"
          >
            {{ item.name }}
          </div>
        </div>
        <div class="addtag">
          <div class="cell" v-if="!addtagshow" @click="addtagbtn">自定义</div>
          <div v-else class="taginputdiv">
            <van-field
              class="taginput"
              v-model="addtag"
              placeholder="请输入标签名称,最多5个字"
              @input="chengtag"
            />
            <van-button
              @click="addtagsave"
              class="tagbutton"
              :disabled="disableds"
              type="primary"
              >确定</van-button
            >
          </div>
        </div>
      </div>
    </div>
    <div class="switch">
      <p>设置默认地址</p>
      <div class="switch_main">
        <div class="main_l">*每次下单回默认推荐使用该地址</div>
        <div class="main_r"><van-switch size="20px" v-model="checked" /></div>
      </div>
    </div>
    <van-button @click="validateForm" class="save" type="primary"
      >保存</van-button
    >
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import areaList from "../../assets/js/areaList";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    name: "custom",
  },
  data() {
    //这里存放数据
    return {
      name: null,
      number: null,
      region: null,
      address: null,
      checked: false,
      showPicker: false,
      disableds: true,
      areaList,
      addtagshow: false,
      addtag: null,
      tagactive: 0,
      taglist: [
        {
          id: 1,
          name: "家",
        },
        {
          id: 2,
          name: "公司",
        },
        {
          id: 3,
          name: "学校",
        },
      ],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    confirmsave(val) {
      for (let i = 0; i < val.length; i++) {
        this.region += val[i].name;
      }
      this.showPicker = false;
    },
    chengtag() {
      if (this.addtag) {
        this.disableds = false;
      } else {
        this.disableds = true;
      }
    },
    addtagbtn() {
      this.addtagshow = true;
    },
    addtagsave() {
      let obj = {
        name: this.addtag,
        id: this.taglist.length,
      };
      this.taglist.push(obj);
      this.addtag = null;
      this.addtagshow = false;
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    validateForm() {
      let that = this;
      that.$refs.form
        .validate()
        .then((success) => {
          that.submit();
        })
        .catch((err) => {});
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang="less">
/* //@import url(); 引入公共css类 */
.custom {
  width: 100%;
  height: 100%;
  background: #f2f2f2;
  .content {
    margin-top: 10px;
  }
  .tag {
    margin-top: 10px;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start;
    padding: 15px;
    background: #ffffff;
    .label {
      width: 40px;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      color: #20253a;
      margin-right: 24px;
      margin-top: 2px;
    }
    .tag_main {
      width: calc(100% - 40px);
      .cell {
        padding: 2px 15px;
        float: left;
        border-radius: 11px;
        border: 1px solid #dadada;
        margin-right: 10px;
        margin-bottom: 12px;
      }
      .active {
        background: #5d5d5d;
        color: #ffffff;
      }
      .addtag {
      }
      .taginputdiv {
        width: 197px;
        height: 22px;
        border: 1px solid #dadada;
        border-radius: 11px;
        overflow: hidden;
        padding: 0 0 0 10px;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        .taginput {
          width: 80%;
          height: 22px;
          line-height: 22px;
          padding: 0;
          font-size: 12px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          text-align: left;
        }
        .tagbutton {
          width: 20%;
          height: 22px;
          display: inline-block;
          background: #5d5d5d;
          font-size: 12px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          text-align: right;
          color: #ffffff;
          border: none;
          border-radius: 0;
          padding: 0;
        }
      }
    }
  }
  .switch {
    padding: 15px;
    background: #ffffff;
    p {
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #20253a;
    }
    .switch_main {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      .main_l {
        font-size: 12px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: left;
        color: #666666;
      }
      .main_r {
      }
    }
  }
  .save {
    width: 90%;
    display: block;
    margin: 0 auto;
    margin-top: 17px;
    background: linear-gradient(180deg, #ff3927, #ff6b4f 133%);
    border-radius: 5px;
    box-shadow: 0px 2px 4px 0px #ffd9d4;
    height: 50px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #ffffff;
    border: none;
  }
}
</style>